<template>
	<view class="index">
		<!-- 导航栏 -->
			
		<view class="bg">
			<view class="search" @tap="toSearch()">
				<uni-icon type="search" ></uni-icon>
				请输入你需要的商品名称
			</view>
		</view>
		<!-- <banner :ad-list="bannerList"></banner> -->
		
		<goods-list :goods="goodsList"  :price-name="priceName" goods-type="tbk"></goods-list>
		<uni-load-more :loadingType="loadingType" ></uni-load-more>
		
	</view>
</template>

<script>
	import uniIcon from '../../components/uni-icon.vue';
	import goodsList from '../../components/coupon-goods-list.vue';
	import UniNavBar from '../../components/uni-nav-bar.vue';
	import	banner from '../../components/banner.vue';
	import base from '../../common/base.js';
	import { priceName } from '../../common/config.js';
	import Request from '../../common/request.js';
	import uniLoadMore from '../../components/uni-load-more.vue'
	
	export default {
		components: {
            uniIcon, goodsList,banner, UniNavBar, uniLoadMore
        },
		data() {
			return {
				keyword: '',
				page: 1,
				bannerList: [],
				goodsList: [],
				nextPage: true,
				loadingType: 0,
			}
		},
		onLoad() {
			uni.showLoading({})
			this.getIndexData(() => {
				uni.hideLoading();
			});
			this.priceName = priceName;
		},
		
		onReachBottom() {
			if(this.keyword) {
				this.searchGoods();
			} else {
				this.getIndexData();
			}
		},
		methods: {
			//获取首页数据
			getIndexData: function(callback) {
				if(this.loadingType == 1 || this.loadingType == 2) {
					return false;
				}
				this.loadingType = 1;
				
				var that = this;
				var params = {
					url: 'tbk/dgOptimusMaterial',
					data: {page: that.page},
					sCallback: function(res) {
					
						that.goodsList = that.goodsList.concat(res.data.goods);
						that.page ++; 
						if(res.data.goods.length < 20) {
							that.loadingType = 2
						}
						that.loadingType = 0;
						
						callback && callback();
					}
				}
				Request.send(params);
			},
			//搜索商品
			searchGoods: function(){
				if(this.loadingType == 1 || this.loadingType == 2) {
					return false;
				}
				this.loadingType = 1;
				var that = this;
				var params = {
					url: 'tbk/dgMaterialOptional',
					data: {
						q: that.keyword,
						page: that.page
					},
					sCallback: function(res) {
						
						that.goodsList = that.goodsList.concat(res.data);
						that.page ++; 
						if(res.length < 20) {
							that.loadingType = 2
						}
					
						that.loadingType = 0;
					}
				}
				Request.send(params);
			},
			
			//搜索
			search: function(){
				var that = this;
				this.goodsList = [];
				this.page = 1;
				this.nextPage = true;
				this.searchGoods();
			},
			
			toSearch: function(){
				uni.navigateTo({
					url: '/pages/coupon/search?type_index=0'
				})
			},
		}
	}
</script>

<style>
	
	view{
		display: flex;
		font-size: 26upx;
	}
	
	
	.index{
		width: 100%;
		flex-direction: column;
		background: #F5F5F5;
		
	}
	
	.bg{
		background: linear-gradient(to right, #FD8E01 0, #FE5C11 100%);
		padding: 60upx 0;
		justify-content: center;
	}
	
	.bg .search{
		background: #fff;
		height: 60upx;
		align-items: center;
		padding: 0 40upx;
		border-radius: 30upx;
		width: 80%;
		color: #929393;
	}
</style>
